package com.shqcjd.sinopecdemomobile.ui.activity.count;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.ui.adapter.LeafAdapter;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class LeafSortActivity extends BaseActivity {


    @BindView(R.id.tv_include_title)
    TextView tvIncludeTitle;
    @BindView(R.id.tv_include_finish)
    Button tvIncludeFinish;
    @BindView(R.id.rv_leaf)
    RecyclerView rvLeaf;
    @BindView(R.id.btn_leaf_start)
    Button btnLeafStart;
    @BindView(R.id.ed_leaf_num)
    EditText edLeafNum;
    @BindView(R.id.btn_leaf_count)
    Button btnLeafCount;
    @BindView(R.id.tv_leaf_sort)
    TextView tvLeafSort;
    @BindView(R.id.ll_leaf_resule)
    LinearLayout llLeafResule;

    private LeafAdapter leafAdapter;
    private Context context;
    private double[] iList = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
    List<Integer> mList = new ArrayList<>();
    double[][] sortResult;
    private List<Integer> quadrantList = new ArrayList<>();//从象限个数8开始  出现所有的情况
    private List<String> resultList = new ArrayList<>();

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_leaf_sort;
    }

    @Override
    public void init() {
        super.init();
        context = this;
    }

    @Override
    public void initView() {
        super.initView();
        tvIncludeTitle.setText("叶片排序");
        tvIncludeFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mList = new ArrayList<>();
        GridLayoutManager g = new GridLayoutManager(context, 4);
        rvLeaf.setLayoutManager(g);
        leafAdapter = new LeafAdapter(mList);
        rvLeaf.setAdapter(leafAdapter);

        btnLeafStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = edLeafNum.getText().toString().trim();
                if(!str.equals("")){
                    int num = Integer.parseInt(str);
                    if(num>0){
                        mList.clear();
                        leafAdapter.clearAllData();
                        for(int i=0; i<num; i++){
                            mList.add(0);
                        }
                        leafAdapter.notifyDataSetChanged();
                    }else{
                        Toast.makeText(context, "请输入正确的叶片数量", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(context, "请输入叶片数量", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btnLeafCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(leafAdapter.getAllEdtextData()!=null){
                        iList = leafAdapter.getAllEdtextData();
                    if(leafAdapter.getAllEdtextData().length>0){
                        resultList.clear();
                        getSortList(iList);
                        initQuadrant(iList.length);
                        for(int i=0; i<quadrantList.size(); i++){
                            resultList.add(leafSort(quadrantList.get(i)));
                        }
                        if(resultList.size()>0){
                            tvLeafSort.setText(resultList.get(0).substring(0,resultList.get(0).length()-2));
                        }
                        createQuadrant();
                    }else{
                        Toast.makeText(context, "请确认所有叶片填写完毕", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(context, "请确认所有叶片填写完毕", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 数组能被8-4的哪一个整除
     */
    private void initQuadrant(int size){
        quadrantList.clear();
        for(int i = 8; i>=4; i--){
            if(size%i==0){
                quadrantList.add(i);
            }
        }
    }

    /**
     * 创建象限
     */
    private void createQuadrant(){
        llLeafResule.removeAllViews();
        for (int i=1; i<resultList.size(); i++){
            View v = LayoutInflater.from(context).inflate(R.layout.item_leaf_result, null);
            TextView tv_leaf_item_quadrant = v.findViewById(R.id.tv_leaf_item_quadrant);
            TextView tv_leaf_item_quadrant_num = v.findViewById(R.id.tv_leaf_item_quadrant_num);
            tv_leaf_item_quadrant.setText("方案"+i+": ");
            tv_leaf_item_quadrant_num.setText(resultList.get(i).substring(0,resultList.get(i).length()-2));
            llLeafResule.addView(v);
        }
    }

    /**
     * 从大到小排序
     * @param mList
     */
    private void getSortList(double[] mList){
        for(int i=0; i<mList.length-1; i++){
            for(int j=0; j<mList.length-1-i; j++){
                if(mList[j]<mList[j+1]){
                    double temp = mList[j+1];
                    mList[j+1] = mList[j];
                    mList[j] = temp;
                }
            }
        }
    }

    /**
     * 叶片排序
     */
    private String leafSort(int qua){
        int index = 0;
        boolean isOrder = true;
        int number = iList.length/qua;
        sortResult = new double[qua][number];
        for(int i=0; i<number;i++){
            for(int j=0; j<qua; j++){
                if(isOrder){
                    sortResult[j][i] = iList[index];
                }else{
                    sortResult[qua-1-j][i] = iList[index];
                }
                index++;
                if(j==qua-1){
                    isOrder = !isOrder;
                }
            }
        }
        String str = "";
        for(int i=0; i<sortResult.length; i++){
            for(int j=0; j<sortResult[i].length; j++){
                str=str+sortResult[i][j]+", ";
            }
        }

//        for(int j=0; j<sortResult[].length; j++){
//            str = str+sortResult[qua-1][j]+", ";
//        }
        return str;
    }
}
